package datos;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.imageio.stream.FileImageInputStream;


import control.Sistema;


public class CargarDeArchivo {

	protected Sistema sis;
	protected File datos = new File ("D:\\Documentos\\Manuel\\Proyecto_Tractores\\Proyecto_Tractores\\src\\datos\\Maquinas.txt");

	
	public CargarDeArchivo(Sistema s){
		sis = s;
		parseFile();
	}
		
	protected void parseFile(){
		int line = 0;
		try{
			FileReader fr = new FileReader(datos);
			BufferedReader buffer= new BufferedReader( fr );
			String str;
			while ((str = buffer.readLine()) != null) {
				this.parseLine(line, str); 
			    line++;
			}
			}catch (IOException e) {
		}
	}
	
	protected void parseLine(int line, String str ){
		String marca = "";
		String modelo = "";
		String anio = "";
		String precio = "";
		String estado = "";
		int cont = 1;
		String aux = "";
		for( int i = 0; i < str.length(); i++ ){
			Character c = new Character(str.charAt(i));
			if((c == '#')||(c == '%')){
				if(c== '#'){
					switch(cont) {
					case 1 : cont++; break ;
					case 2 : modelo = aux.toLowerCase(); aux = ""; cont++; break;
					case 3 : precio = aux.toLowerCase(); aux = ""; cont++; break;
					case 4 : anio = aux.toLowerCase(); aux = ""; cont++; break;
					case 5 : marca = aux.toLowerCase(); aux = ""; cont++; break;
					case 6 : estado = aux.toLowerCase(); aux = ""; cont++; 
					System.out.println(anio);
						sis.addTractor(marca,modelo,estado,anio,precio); 
					}
				}else{
					sis.addFoto(aux);
					aux = "";
					}
			}else{
				aux += c;
			}	
		}
	}
	
	public void Actualizar(){
		parseFile();
	}
	
	
}